import React, { useState, useEffect } from 'react';
import { uDb, IkFind, NuiDlg, obj_str, cline, useForm, ck_empty } from 'sui';
import { g_db } from 'db/state';

const DlgTech = ({ umd, open, setOpen }) => {
    const [value, setValue] = useState();
    const [lst_tech, setTech] = useState();
    const form = useForm(setValue);

    useEffect(() => {
        if (!open) {
            return;
        }

        uDb(null, "tech/list", { line: cline() },
            r => {
                r.forEach(d => d["id"] = obj_str(d, "sn"));
                setTech(r);
            }, e => setTech(null));

        setValue(null);
    }, [open]);

    const hdone = () => {
        const ck = {
            tech: v => ck_empty(v, "工艺"),
        };

        if (!form.hcheck(ck, value)) {
            return;
        }

        const tech_sn = obj_str(value, "tech");

        g_db.s_tech(tech_sn);

        umd.mset({
            "tech": tech_sn,
        });

        setOpen(false);
    };

    return (
        <NuiDlg open={open} title="设置型号" hcmd={hdone} labd="确定">
            <IkFind label="型号" value={value} form={form} name="tech" lst={lst_tech}/>
        </NuiDlg>
    );
};

export {DlgTech};
